//
//  LoginViewController.m
//  享动
//
//  Created by lanou on 16/4/6.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "LoginViewController.h"
#import "registerViewController.h"
#import "RootTabBarViewController.h"
#import "StartViewController.h"
#import "RequestManager.h"
#import "RootTabBarViewController.h"
#import "UserModel.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *bigView;
@property (weak, nonatomic) IBOutlet UIImageView *lastDownView;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet UITextField *nameT;
@property (weak, nonatomic) IBOutlet UITextField *paswordT;

@property (weak, nonatomic) IBOutlet UIButton *qqButton;
@property (weak, nonatomic) IBOutlet UIButton *wbButton;
@property (weak, nonatomic) IBOutlet UIButton *wxButton;

@property(nonatomic,strong)CIFilter *filter;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UILabel *otherWay;

@property (weak, nonatomic) IBOutlet UIButton *skipbutton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.lastDownView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
    self.bigView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
    
    // 第一次打开有动画效果
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        // 进来hidden为NO
        self.bigView.alpha = 0;
        
        [self performSelector:@selector(mohu) withObject:nil afterDelay:3];
        
        
        [UIView animateWithDuration:3 animations:^{
            self.bigView.alpha = 0.5;
        }];
        
    }
    else
    {
        self.bigView.alpha=0.5;
    }
    
    
    
    
    
    self.titleLabel.frame = CGRectMake(50, KHEIGHT/6.6, KWIDTH-100, KHEIGHT/9);
    
    
    
    self.mid.backgroundColor = [UIColor colorWithRed:92/255.0 green:94/255.0 blue:102/255.0 alpha:1];
    self.mid.frame = CGRectMake(5,KHEIGHT / 9 +KHEIGHT/6.6+50, KWIDTH-10, 100);
    
    self.registerButton.frame = CGRectMake((KWIDTH-120)/3, self.mid.frame.origin.y +100 +KHEIGHT/13, 60, 40);

    self.loginButton.frame = CGRectMake((KWIDTH -120)/3*2+60, self.mid.frame.origin.y +100 +KHEIGHT/13, 60, 40);
    
    self.otherWay.frame = CGRectMake((KWIDTH - 100)/2,KHEIGHT -100, 100, 25);
 
    
    //self.qqButton.backgroundColor = [UIColor redColor];
    
    self.qqButton.frame = CGRectMake((KWIDTH - 95)/2, KHEIGHT -70, 25, 25);
    self.wbButton.frame = CGRectMake((KWIDTH - 95)/2+35, KHEIGHT-70, 25, 25);
    self.wxButton.frame = CGRectMake((KWIDTH - 95)/2+70, KHEIGHT-70, 25, 25);
    
    
    self.skipbutton.frame = CGRectMake((KWIDTH-120)/2, KHEIGHT-40, 120, 30);
    
    
    
    
    
    
    self.nameT.placeholder = @"请输入邮箱";
    [self.nameT setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
     self.nameT.clearButtonMode = YES;
    [self.nameT setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    
    
    self.paswordT.placeholder = @"请输入密码";
    [self.paswordT setSecureTextEntry:YES];
    [self.paswordT setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.paswordT setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    
    
    [_nameT setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_paswordT setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];



    
    [self.registerButton addTarget:self action:@selector(registerB) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

#pragma mark --- 点击登录button 触发的方法 ---
- (IBAction)loginButton:(id)sender {
    
    NSDictionary *parDic = @{@"_ch":@"appStore", @"_lan":@"ch", @"_nw":@"WIFI", @"_product":@"fitmix", @"_sdk":@"iPhone%20OS8.2",@"_v":@"3", @"email":[_nameT.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], @"password":[_paswordT.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ,@"uid":@"0"};
    
    [RequestManager requestWithUrl:@"http://appt.igeekery.com/user/login.json" requestType:RequestTypePost parDic:parDic finish:^(NSData *data)
     {
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSString *msg = [dic objectForKey:@"msg"];
         NSDictionary *userDic = [dic objectForKey:@"user"];
         UserModel *model = [[UserModel alloc] init];
         [model setValuesForKeysWithDictionary:userDic];
         NSMutableArray *array = [NSMutableArray array];
         [array addObject:model];
         
         NSLog(@"%@", dic);
         dispatch_async(dispatch_get_main_queue(), ^{
             
             if (msg == nil)
             {
                 
                 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
                 
                 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
                 
                 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                 {
                     
                     RootTabBarViewController *rootVc = [[RootTabBarViewController alloc] init];
                     [[NSUserDefaults standardUserDefaults] setObject:_nameT.text forKey:@"name"];
                     [[NSUserDefaults standardUserDefaults] setObject:_paswordT.text forKey:@"password"];
                     [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld", model.myId] forKey:@"id"];
                     [[NSUserDefaults standardUserDefaults] setObject:model.name forKey:@"userName"];
                     
                     
                     [self presentViewController:rootVc animated:YES completion:nil];
                     
                 }];
                 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                 [alert addAction:okAction];
                 [alert addAction:cancelAction];
                 [self presentViewController:alert animated:YES completion:nil];
             }
             else
             {
                 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
                 
                 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                 }];
                 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                 [alert addAction:okAction];
                 [alert addAction:cancelAction];
                 [self presentViewController:alert animated:YES completion:nil];
             }
         });
         
     }error:^(NSError *error)
     {
         NSLog(@"注册网络请求错误");
     }];
}

- (void)viewWillAppear:(BOOL)animated
{
     self.navigationController.navigationBar.hidden = YES;
}

- (IBAction)skip:(id)sender
{
    RootTabBarViewController *rootVc = [[RootTabBarViewController alloc]init];
    [self presentViewController:rootVc animated:YES completion:nil];
    
}

- (void)registerB
{
    registerViewController * regis = [[registerViewController alloc]initWithNibName:@"register" bundle:[NSBundle mainBundle]];
    [self presentViewController:regis animated:YES completion:nil];
    
    
}
- (void)mohu

{
    [UIView animateWithDuration:1 animations:^{
        CIContext *context = [CIContext contextWithOptions:nil];
        CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
        
        // create gaussian blur filter
        _filter = [CIFilter filterWithName:@"CIGaussianBlur"];
        [_filter setValue:inputImage forKey:kCIInputImageKey];
        [_filter setValue:[NSNumber numberWithFloat:3.0] forKey:@"inputRadius"];
        // blur image
        CIImage *result = [_filter valueForKey:kCIOutputImageKey];
        CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
        UIImage *image = [UIImage imageWithCGImage:cgImage];
        CGImageRelease(cgImage);
        
        self.lastDownView.image = image;
        
    }];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.nameT resignFirstResponder];
    [self.paswordT resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    if (textField == _nameT) {
        [_paswordT becomeFirstResponder];
    }

    return [textField resignFirstResponder];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
